﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using Common.Logging;

public partial class map_in : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private SessionHelper m_SessionHelper;

    private int m_ParentNodeId = 40;
    //合作單位討論區
    private int m_NodeId = 41;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_SessionHelper = new SessionHelper();
        m_NodeId = QueryStringHelper.GetInteger("node", 0);

        if (m_SessionHelper.Member == null)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("請先登入會員。", "login.aspx"), false);
            return;
        }

        if (!IsPostBack)
        {           
            if (m_NodeId == 0)
            {
                IList<NodeVO> nodeList = m_PostService.GetNodeListByParentId(m_ParentNodeId);                
                if (nodeList != null && nodeList.Count() > 0)
                {                    
                    Response.Redirect(string.Format("discuss.aspx?node={0}", nodeList[0].NodeId));
                }
            }

            NodeVO nodeVO = m_PostService.GetNodeById(m_NodeId);
            if (nodeVO != null)
            {
                ltlName.Text = nodeVO.Name;
            }
        }
    }        

    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (m_SessionHelper.Member == null)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("請先登入會員。", "login.aspx"), false);
            return;
        }

        PostVO postVO = new PostVO();
        UIHelper.FillVO(PanelUI, postVO);
        postVO.Node = m_PostService.GetNodeById(m_NodeId);
        postVO.CreatedDate = DateTime.Now;
        postVO.CreatedBy = m_SessionHelper.Member.LoginId;
        postVO.Flag = 1;
        postVO = m_PostService.CreatePost(postVO);

        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("發文成功。", string.Format("discuss_in.aspx?post={0}", postVO.PostId)), false);
    }
}